# -*- coding:utf-8 -*-
import wx
import wx.grid as WG
from AddDialog import Mstsc_Pops,Mstsc
import json,os


class VpsGrid(WG.GridTableBase):
    def __init__(self):
        WG.GridTableBase.__init__(self)

        # 标题
        self.colLabels = ["名称","计算机","用户","密码","备注"]

        #添加数据源
        self.Data = [
            # ['Mask 1', 1, "2.5","320.6",(200,20,100)],
            # ['Mask 2', 1, "2.5","320.6",(50,0,200)]
        ]

    # 自定义的表格必须覆盖原函数
    def GetNumberRows(self):
        return len(self.Data)

    def GetNumberCols(self):
        return len(self.colLabels)

    def GetValue(self, row, col):
        return self.Data[row][col]

    def SetValue(self, row, col, value):
        self.Data[row][col] = value

    # 输入表格Label
    def GetColLabelValue(self, col):
        return self.colLabels[col]

    # 自定义函数进行添加数据
    def InsertRow(self, row):
        '''
        插入数据到指定的位置,并更新表格
        '''
        self.Data.append(row)

        #  返回传递给 的最后一个网格SetView。 增加网格的批次计数
        self.GetView().BeginBatch()
        
        # 行插入的消息 网格表用于向网格视图发送请求和通知的消息类。
        msg = WG.GridTableMessage(self,WG.GRIDTABLE_NOTIFY_ROWS_INSERTED,
            0,1)

        # 接收并处理来自表的消息
        self.GetView().ProcessTableMessage(msg)
       
        # 减少网格的批次计数
        self.GetView().EndBatch()
        # cell 值如果有更改的消息 更新数据
        msg = WG.GridTableMessage(self, WG.GRIDTABLE_REQUEST_VIEW_GET_VALUES)
        self.GetView().ProcessTableMessage(msg)

    def DeleteRow(self,DelRowNum):
        try:
            # 数据删除
            print(DelRowNum)
            self.Data.pop(DelRowNum)
            # self.Data.remove(DelRowNum)
        except IndexError as IE:
            pass
        else:
            self.GetView().BeginBatch()
            
            msg = WG.GridTableMessage(self,WG.GRIDTABLE_NOTIFY_ROWS_DELETED,
                DelRowNum,1)
     
            self.GetView().ProcessTableMessage(msg)
           
            self.GetView().EndBatch()
            msg = WG.GridTableMessage(self, WG.GRIDTABLE_REQUEST_VIEW_GET_VALUES)
            self.GetView().ProcessTableMessage(msg)

    def OpenMstsc(self,Data):
        MstscName,Ipx,IpxUser,IpxPassWd,RE = Data
        # print(MstscName,Ipx,IpxUser,IpxPassWd,RE)
        # 进入远程
        Mstsc(Ipx,IpxPassWd,IpxUser)
    
    def UpMstsc(self,RowNum):
        if RowNum > 0:
            self.Data[RowNum],self.Data[RowNum-1] = self.Data[RowNum-1],self.Data[RowNum]

    
    def DownMstsc(self,RowNum):
        self.Data[RowNum],self.Data[RowNum+1] = self.Data[RowNum+1],self.Data[RowNum]



class CustomGrid(WG.Grid):
    def __init__(self, parent, id, rowLabelSize = 0, customGridTable = None):
        '''
        主要表格信息
        导入主窗口
        设置ID
        '''
        WG.Grid.__init__(self, parent,id)
        # 主窗口的导入
        self.parent = parent

        self.RowLabelSize = rowLabelSize
        self.__CustomTableSource = customGridTable
        self.SetTable(self.__CustomTableSource, True)
        
        # 表格设置
        self.__InitStyle()
        # 设置表格不能调整行高
        self.EnableDragRowSize(False)

        # 设置可编辑/可读
        self.EnableEditing(False)
        
        # 显示表格线 
        self.EnableGridLines(True)

        # 表格事件
        # 表格选中事件触发
        self.Bind(WG.EVT_GRID_SELECT_CELL, self.__OnCellSelected)
        self.Bind(WG.EVT_GRID_CELL_RIGHT_CLICK, self.__OnRightClick)
        self.Bind(WG.EVT_GRID_CELL_LEFT_CLICK, self.__OnLeftClick)

        self.Bind(WG.EVT_GRID_RANGE_SELECT, self.__OnChoose)

        # 双击打开远程
        self.Bind(WG.EVT_GRID_CELL_LEFT_DCLICK, self.__OnTwoClick)

        # 添加键盘监听
        self.Bind(wx.EVT_KEY_DOWN,self.__onKeyDown)

        # 修改列宽
        self.SetColSize(0, 100) # 指定列宽
        self.SetColSize(1, 150) # 指定列宽
        self.SetColSize(2, 100) # 指定列宽
        self.SetColSize(3, 100) # 指定列宽
        self.SetColSize(4, 140) # 指定列宽

        # 参数变量
        # 当前选择的是第几行
        self.SnumRow = -1

    # 设置选中后的颜色
    def __InitStyle(self):
        pass
        # 右键点击失效
        # self.SetSelectionBackground('red')

    # 鼠标左键双击打开远程
    def __OnTwoClick(self,event):
        self.SnumRow = event.Row
        Table = self.GetTable()
        Table.OpenMstsc(Table.Data[self.SnumRow])
    
    # 选中单元格后全行选择
    def __OnCellSelected(self,event):
        self.SnumRow = event.Row
        # 设置全行为选中状态
        self.SelectRow(event.Row)

        # RC = self.GetRowsCount()
        # print(RC)
        # attr = WG.GridCellAttr()
        # 设置单元格为只读
        # attr.SetReadOnly(isReadOnly=True)
        # self.SetRowAttr(event.Row,attr)
    
    # 鼠标左键点击事件
    def __OnLeftClick(self,event):
        self.__ButtonOpen()
        self.SnumRow = event.Row
        # 设置全行为选中状态
        self.SelectRow(event.Row)

    # 鼠标右键点击菜单
    def __OnRightClick(self,event):
        self.__ButtonOpen()
        self.SnumRow = event.Row
        self.SelectRow(event.Row)
        print(event.GetPosition(),event.Row)
        self.PopupMenu(MyPopupMenu(self,event.Row),event.GetPosition())
    
    # 改变当前列表中按钮状态
    def __ButtonOpen(self):
        # 使按钮变的可用
        self.parent.Start.Enable(True)
        self.parent.Xiugai.Enable(True)
        self.parent.shanchu.Enable(True)
        self.parent.biaoji.Enable(True)
        self.parent.shangyi.Enable(True)
        self.parent.xiayi.Enable(True)

    # 键盘监听
    def __onKeyDown(self,event):
        if self.SnumRow >=1 :
            if event.GetKeyCode() == 315:
                # print('上')
                self.SnumRow -=1
            elif event.GetKeyCode() == 317:
                self.SnumRow +=1
                # print('下')
        elif self.SnumRow <= 0:
            if event.GetKeyCode() == 317:
                self.SnumRow +=1
            elif event.GetKeyCode() == 315:
                self.SnumRow = 0

        if self.SnumRow >= self.GetNumberRows()-1:
            self.SnumRow = self.GetNumberRows()-1

        # print(self.SnumRow)
        # 设置选择状态
        self.SelectRow(self.SnumRow)
        self.__ButtonOpen()

        table  = self.GetTable()
        if event.GetKeyCode() == 370 or event.GetKeyCode() == 13:
            # print(self.SnumRow)
            if self.SnumRow != -1:
                print(" == 进入 ==")
                table.OpenMstsc(table.Data[self.SnumRow])

        if event.GetKeyCode() == 127:
            if self.SnumRow != -1:
                print(" == 删除 ==")
                # 有一个提示没写
                TC = wx.MessageDialog(None,"确认是否删除?","删除",wx.ICON_QUESTION|wx.YES_NO)
                if TC.ShowModal() == wx.ID_YES:
                    # 当前选择的数据行
                    table.DeleteRow(self.SnumRow)
                TC.Destroy()
        
        # 设置选择状态
        # self.SelectRow(SnumRow)
        # 317 下
        # 315 上
        # 127 del
        # 13/370 enter

    def __OnChoose(self,event):
        pass
        # print(event.GetBottomRightCoords(),'T_T')
        # print(event.GetBottomRow(),'T_T')
        # print(event.Selecting())
        # print(dir(event))
        
    # 获取当前选择的表格
    def __Row(self):
        return self.SnumRow
    
    # 写入当前处于的最新列
    def __SetRow(self,NRow):
        self.SnumRow = NRow



# 右键菜单
class MyPopupMenu(wx.Menu):
    def __init__(self,parent,RowNum):
        '''
        传入 Grid 的信息
        当前选择的行数
        '''
        super(MyPopupMenu, self).__init__()

        self.grid = parent
        self.RowNum = RowNum
        self.table = self.grid.GetTable()

        Start = wx.Image(os.getcwd() + "\\icon_\\" + "Start02.png", wx.BITMAP_TYPE_PNG)
        NStart = Start.Rescale(width=16,height=16)

        Xiugai = wx.Image(os.getcwd() + "\\icon_\\" + "Xiugai02.png", wx.BITMAP_TYPE_PNG)
        NXiugai = Xiugai.Rescale(width=16,height=16)
        
        shanchu = wx.Image(os.getcwd() + "\\icon_\\" + "shanchu02.png", wx.BITMAP_TYPE_PNG)
        Nshanchu = shanchu.Rescale(width=16,height=16)
        
        biaoji = wx.Image(os.getcwd() + "\\icon_\\" + "biaoji02.png", wx.BITMAP_TYPE_PNG)
        Nbiaoji = biaoji.Rescale(width=16,height=16)
        
        shangyi = wx.Image(os.getcwd() + "\\icon_\\" + "shangyi02.png", wx.BITMAP_TYPE_PNG)
        Nshangyi = shangyi.Rescale(width=16,height=16)
        
        xiayi = wx.Image(os.getcwd() + "\\icon_\\" + "xiayi02.png", wx.BITMAP_TYPE_PNG)
        Nxiayi = xiayi.Rescale(width=16,height=16)

        mmi = wx.MenuItem(self, wx.NewId(), '连接')
        mmi.SetBitmap(NStart.ConvertToBitmap())
        self.Bind(wx.EVT_MENU, self.OnStart, mmi)
        self.Append(mmi)
                
        cmi = wx.MenuItem(self, wx.NewId(), '编辑')
        cmi.SetBitmap(NXiugai.ConvertToBitmap())
        # self.Bind(wx.EVT_MENU, self.OnDelData, cmi)
        self.Append(cmi)

        cmi = wx.MenuItem(self, wx.NewId(), '删除')
        cmi.SetBitmap(Nshanchu.ConvertToBitmap())
        self.Bind(wx.EVT_MENU, self.OnDelData, cmi)
        self.Append(cmi)

        imp = wx.Menu()
        imp.Append(wx.ID_ANY, '无标记')
        for x in range(1,6):
            diannao = wx.Image(os.getcwd() + "\\icon_\\" + "diannao%d.png"%x, wx.BITMAP_TYPE_PNG)
            Ndiannao = diannao.Rescale(width=16,height=16)
            dn = wx.MenuItem(self, wx.NewId(), '标记%d'%x)
            dn.SetBitmap(Ndiannao.ConvertToBitmap())
            self.Bind(wx.EVT_MENU, self.OnMark, dn)
            imp.Append(dn)

        # imp.Append(wx.ID_ANY, '标记1')
        # imp.Append(wx.ID_ANY, '标记2')
        # imp.Append(wx.ID_ANY, '标记3')
        # imp.Append(wx.ID_ANY, '标记4')
        # imp.Append(wx.ID_ANY, '标记5')

        # self.Append(wx.ID_ANY, 'Import', imp)

        # cmi = wx.MenuItem(imp, wx.NewId(), '标记')
        # cmi.SetBitmap(Nshangyi.ConvertToBitmap())
        # print(dir(imp))
        self.AppendSubMenu(imp,'标记')

        cmi = wx.MenuItem(self, wx.NewId(), '上移')
        cmi.SetBitmap(Nshangyi.ConvertToBitmap())
        self.Append(cmi)
        self.Bind(wx.EVT_MENU, self.OnUp, cmi)

        cmi = wx.MenuItem(self, wx.NewId(), '下移')
        cmi.SetBitmap(Nxiayi.ConvertToBitmap())
        self.Append(cmi)
        self.Bind(wx.EVT_MENU, self.OnDown, cmi)
 
    def OnMinimize(self, e):
        pass
 
    def OnClose(self, e):
        pass

    def OnStart(self, e):
        self.table.OpenMstsc(self.table.Data[self.RowNum])

    def OnDelData(self, e):
        print(">_<!!")
        TC = wx.MessageDialog(None,"确认是否删除?","删除",wx.ICON_QUESTION|wx.YES_NO)
        if TC.ShowModal() == wx.ID_YES:
            # 当前选择的数据行
            self.table.DeleteRow(self.RowNum)
        TC.Destroy()

    # 标记点击
    def OnMark(self,e):
        pass
        # print(e)
        # print(dir(e))
        # print(e.GetClientData())
    
    def OnUp(self, e):        
        self.table.UpMstsc(self.RowNum)
        if self.RowNum != 0:
            self.grid.SelectRow(self.RowNum-1)
            self.grid._CustomGrid__SetRow(self.RowNum-1)
    
    def OnDown(self, e):
        if self.RowNum >= len(self.table.Data) - 1:
            pass
        else:
            self.table.DownMstsc(self.RowNum)
            self.grid.SelectRow(self.RowNum+1)
            self.grid._CustomGrid__SetRow(self.RowNum+1)

#主窗口
class TestFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None,title="GridTable",pos=(700,400),size = wx.Size( 615,350 )) # ,size=wx.Size(500,500)
        sizer = wx.BoxSizer(wx.VERTICAL)
        self.table = VpsGrid()
        # addButton = wx.Button(self, -1, "Add")
        # deleteButton = wx.Button(self, -1, "Delete")

        # 添加图标
        # self.Icon = wx.Icon(name=os.getcwd() + "\\icon_\\" + 'El.ico',type=wx.BITMAP_TYPE_ICO)
        self.Icon = wx.Icon(name=os.getcwd() + "\\icon_\\" + 'El2.ico',type=wx.BITMAP_TYPE_ICO)
        self.SetIcon(self.Icon)

        # 导航栏信息 ==========>>>>
        self.m_toolBar = wx.ToolBar(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TB_HORIZONTAL)
        # 添加背景色
        self.m_toolBar.SetBackgroundColour(wx.Colour(16316664))
        # 导入导航格式
        self.NavigationFormat()
        self.m_toolBar.Realize()
        self.m_toolBar.SetToolPacking(8)

        # sizer.Add(addButton, 0, wx.SHAPED)
        # sizer.Add(deleteButton, 0, wx.SHAPED)
        # 添加导航栏
        sizer.Add(self.m_toolBar, 0, wx.ALL|wx.EXPAND,0)
        
        # addButton.Bind(wx.EVT_BUTTON, self.OnAddClick)
        # deleteButton.Bind(wx.EVT_BUTTON, self.OnDeleteClick)

        # 表格信息 ==========>>>>
        grid = CustomGrid(self, -1, customGridTable = self.table)
        self.grid = grid
        mainSizer = wx.BoxSizer(wx.VERTICAL)
        # self.grid.Enable(False)
        
        # mainSizer.Add(sizer)
        mainSizer.Add(grid, 1, wx.ALL|wx.EXPAND)
        # print(dir(mainSizer))
        # 修改窗口大小
        # mainSizer.SetMinSize((580,400))

        self.num = 0

        # self.SetSizerAndFit(sizer)
        sizer.Add(mainSizer,1,wx.EXPAND,5)
        self.SetSizer(sizer)
        self.Layout()

        # 底部状态栏
        self.BotBar =  self.CreateStatusBar(2,wx.STB_DEFAULT_STYLE,wx.ID_ANY)
        self.BotBar.SetStatusWidths([-1, 35])

        # 导入数据
        self.ImportData()
        self.Show(True)

    # 添加
    def OnAddClick(self,event):
        # 返回指向当前表对象的基指针
        # table  = self.grid.GetTable()
        # self.num += 1
        # table.InsertRow(['insert index ', self.num, "2.5","110.6",(50,200,30)])
        # 当前的全部数据
        # print(self.grid.GetTable().Data)
        # 刷新表格
        # self.grid.Refresh()
        
        # 添加弹窗
        MstscAdd = Mstsc_Pops(self.grid)
        Current_Vps = MstscAdd.Current_Vps
        print(Current_Vps)
        self.grid.SelectRow(len(self.table.Data)-1)
        self.grid._CustomGrid__SetRow(len(self.table.Data)-1)

        # 更新下部状态栏
        self.BotBar.SetStatusText("共{}项".format(len(self.table.Data)),i=1)

    # 修改
    def OnReviseClick(self,event):
        RowNum = self.grid._CustomGrid__Row()
        Mstsc = Mstsc_Pops(self.grid,RowNum,self.table.Data[RowNum])

    # 删除数据
    def OnDeleteClick(self, event):
        # print(table)
        # 当前选择的数据行
        # TC = wx.MessageDialog(self,"确认是否删除?","删除 ===>>>",wx.ICON_WARNING|wx.YES_NO) # 黄色感叹
        # TC = wx.MessageDialog(self,"确认是否删除?","删除 ===>>>",wx.ICON_NONE|wx.YES_NO) # 无显示图标
        # TC = wx.MessageDialog(self,"确认是否删除?","删除 ===>>>",wx.ICON_ERROR|wx.YES_NO) # 红色删除
        TC = wx.MessageDialog(self,"确认是否删除?","删除",wx.ICON_QUESTION|wx.YES_NO) # 无显示图标 + 声响
        # TC = wx.MessageDialog(self,"确认是否删除?","删除 ===>>>",wx.ICON_INFORMATION|wx.YES_NO) # 蓝色感叹
        # TC = wx.MessageDialog(self,"确认是否删除?","删除 ===>>>",wx.ICON_EXCLAMATION|wx.YES_NO) # 黄色感叹
        # TC = wx.MessageDialog(self,"确认是否删除?","删除",wx.ICON_HAND|wx.YES_NO) # 红色删除
        # TC = wx.MessageDialog(self,"确认是否删除?","删除 ===>>>",wx.ICON_AUTH_NEEDED|wx.YES_NO) # win盾
        # wx.ID_YES,wx.ID_NO
        if TC.ShowModal() == wx.ID_YES:
            DelRowNum = self.grid._CustomGrid__Row()
            self.table.DeleteRow(DelRowNum)
        TC.Destroy()

        # 更新下部状态栏
        self.BotBar.SetStatusText("共{}项".format(len(self.table.Data)),i=1)

    # 打开远程
    def OnStartClick(self,event):
        # 默认选择 -> 0
        RowNum = self.grid._CustomGrid__Row()
        self.table.OpenMstsc(self.table.Data[RowNum])
    
    # 上移
    def OnUpClick(self,event):
        # 默认选择 -> 0
        RowNum = self.grid._CustomGrid__Row()
        if RowNum >= len(self.table.Data):
            pass
        else:
            self.table.UpMstsc(RowNum)
            if RowNum != 0:
                self.grid.SelectRow(RowNum-1) # ,addToSelected=True 多选O_O
                # 更新选中行数
                self.grid._CustomGrid__SetRow(RowNum-1)
    
    # 下移
    def OnDownClick(self,event):
        # 默认选择 -> 0
        RowNum = self.grid._CustomGrid__Row()
        if RowNum >= len(self.table.Data) - 1:
            pass
        else:
            self.table.DownMstsc(RowNum)
            self.grid.SelectRow(RowNum+1)
            # 更新选中行数
            self.grid._CustomGrid__SetRow(RowNum+1)
            # self.grid.SelectAll() # 全选
            # self.grid.ClearSelection() # 删除选中
    
    # 导入数据
    def ImportData(self):
        with open("Mstsc_data.json",'r') as f:
            MD = f.read()
        self.MD = json.loads(MD)
        table  = self.grid.GetTable()
        for data in self.MD:
            table.InsertRow([data['name'], data['ipx'], data['user'],data['passwd'],data['RE']])

        self.BotBar.SetStatusText("共{}项".format(len(self.MD)),i=1)
        

    def AddFocus(self,event):
        if event.Id == 1000:
            self.Start.SetBackgroundColour(wx.Colour(16316664))
        elif event.Id == 1001:
            self.Add.SetBackgroundColour(wx.Colour(16316664))
        elif event.Id == 1002:
            self.Xiugai.SetBackgroundColour(wx.Colour(16316664))
        elif event.Id == 1003:
            self.shanchu.SetBackgroundColour(wx.Colour(16316664))
        elif event.Id == 1004:
            self.chazhao.SetBackgroundColour(wx.Colour(16316664))
        elif event.Id == 1005:
            self.biaoji.SetBackgroundColour(wx.Colour(16316664))
        elif event.Id == 1006:
            self.shangyi.SetBackgroundColour(wx.Colour(16316664))
        elif event.Id == 1007:
            self.xiayi.SetBackgroundColour(wx.Colour(16316664))
        elif event.Id == 1008:
            self.shezhi.SetBackgroundColour(wx.Colour(16316664))
                                 
        event.Skip()

    def AddKill(self,event):
        if event.Id == 1000:
            self.Start.SetBackgroundColour(wx.Colour(15066597))
        elif event.Id == 1001:
            self.Add.SetBackgroundColour(wx.Colour(15066597))
        elif event.Id == 1002:
            self.Xiugai.SetBackgroundColour(wx.Colour(15066597))
        elif event.Id == 1003:
            self.shanchu.SetBackgroundColour(wx.Colour(15066597))
        elif event.Id == 1004:
            self.chazhao.SetBackgroundColour(wx.Colour(15066597))
        elif event.Id == 1005:
            self.biaoji.SetBackgroundColour(wx.Colour(15066597))
        elif event.Id == 1006:
            self.shangyi.SetBackgroundColour(wx.Colour(15066597))
        elif event.Id == 1007:
            self.xiayi.SetBackgroundColour(wx.Colour(15066597))
        elif event.Id == 1008:
            self.shezhi.SetBackgroundColour(wx.Colour(15066597))
        event.Skip()

        # self.m_bpButton1 = wx.BitmapButton( self.m_toolBar1, wx.ID_ANY, wx.Bitmap( u"D:\\My_ptoject\\2021\\MstscManager\\icon_\\chazhao.png", wx.BITMAP_TYPE_ANY ), wx.DefaultPosition, wx.DefaultSize, wx.BU_BOTTOM )

    # 导航栏各种事件合计
    def DHJD(self):
        # 鼠标移入/移出事件
        # 获取焦点/失去焦点
        self.Start.Bind(wx.EVT_ENTER_WINDOW,self.AddKill)
        self.Start.Bind(wx.EVT_LEAVE_WINDOW,self.AddFocus)

        self.Add.Bind(wx.EVT_ENTER_WINDOW,self.AddKill)
        self.Add.Bind(wx.EVT_LEAVE_WINDOW,self.AddFocus)

        self.Xiugai.Bind(wx.EVT_ENTER_WINDOW,self.AddKill)
        self.Xiugai.Bind(wx.EVT_LEAVE_WINDOW,self.AddFocus)

        self.shanchu.Bind(wx.EVT_ENTER_WINDOW,self.AddKill)
        self.shanchu.Bind(wx.EVT_LEAVE_WINDOW,self.AddFocus)

        self.chazhao.Bind(wx.EVT_ENTER_WINDOW,self.AddKill)
        self.chazhao.Bind(wx.EVT_LEAVE_WINDOW,self.AddFocus)

        self.biaoji.Bind(wx.EVT_ENTER_WINDOW,self.AddKill)
        self.biaoji.Bind(wx.EVT_LEAVE_WINDOW,self.AddFocus)

        self.shangyi.Bind(wx.EVT_ENTER_WINDOW,self.AddKill)
        self.shangyi.Bind(wx.EVT_LEAVE_WINDOW,self.AddFocus)

        self.xiayi.Bind(wx.EVT_ENTER_WINDOW,self.AddKill)
        self.xiayi.Bind(wx.EVT_LEAVE_WINDOW,self.AddFocus)

        self.shezhi.Bind(wx.EVT_ENTER_WINDOW,self.AddKill)
        self.shezhi.Bind(wx.EVT_LEAVE_WINDOW,self.AddFocus)

        # 不可选中时展示的视图
        Startx = wx.Image(os.getcwd() + "\\icon_\\" + "Start.png", wx.BITMAP_TYPE_PNG)
        # NStart = Start.Rescale()
        NStart2 = Startx.Rescale(width=18,height=18)
        # 当按钮不能选中时的位图
        self.Start.SetBitmapDisabled(NStart2.ConvertToBitmap())
        
        xiugai = wx.Image(os.getcwd() + "\\icon_\\" + "xiugai.png", wx.BITMAP_TYPE_PNG)
        # NStart = Start.Rescale()
        Nxiugai = xiugai.Rescale(width=18,height=18)
        # 当按钮不能选中时的位图
        self.Xiugai.SetBitmapDisabled(Nxiugai.ConvertToBitmap())
        
        shanchux = wx.Image(os.getcwd() + "\\icon_\\" + "shanchu001.png", wx.BITMAP_TYPE_PNG)
        # NStart = Start.Rescale()
        Nshanchux = shanchux.Rescale(width=18,height=18)
        # 当按钮不能选中时的位图
        self.shanchu.SetBitmapDisabled(Nshanchux.ConvertToBitmap())
        
        shangyix = wx.Image(os.getcwd() + "\\icon_\\" + "shangyi.png", wx.BITMAP_TYPE_PNG)
        # NStart = Start.Rescale()
        Nshangyix = shangyix.Rescale(width=18,height=18)
        # 当按钮不能选中时的位图
        self.shangyi.SetBitmapDisabled(Nshangyix.ConvertToBitmap())

        xiayi = wx.Image(os.getcwd() + "\\icon_\\" + "xiayi.png", wx.BITMAP_TYPE_PNG)
        # NStart = Start.Rescale()
        Nxiayi = xiayi.Rescale(width=18,height=18)
        # 当按钮不能选中时的位图
        self.xiayi.SetBitmapDisabled(Nxiayi.ConvertToBitmap())

        # 禁止选中
        self.Start.Enable(False)
        self.Xiugai.Enable(False)
        self.shanchu.Enable(False)
        self.biaoji.Enable(False)
        self.shangyi.Enable(False)
        self.xiayi.Enable(False)

        # 导航栏点击事件
        # EVT_BUTTON  EVT_LEFT_DOWN
        self.Start.Bind(wx.EVT_LEFT_DOWN, self.OnStartClick)
        self.Add.Bind(wx.EVT_LEFT_DOWN, self.OnAddClick)
        self.Xiugai.Bind(wx.EVT_LEFT_DOWN, self.OnReviseClick)
        self.shanchu.Bind(wx.EVT_LEFT_DOWN, self.OnDeleteClick)
        self.shangyi.Bind(wx.EVT_LEFT_DOWN, self.OnUpClick)
        self.xiayi.Bind(wx.EVT_LEFT_DOWN, self.OnDownClick)

    def OnWriteJson(self,event):
        Data = [{"name":name,"ipx":ipx,"user":user,"passwd":passwd,"RE":RE} for name,ipx,user,passwd,RE in self.table.Data]
        
        TC = wx.MessageDialog(self,"确认更新Josn?","Update Json",wx.ICON_QUESTION|wx.YES_NO) # 无显示图标 + 声响
        if TC.ShowModal() == wx.ID_YES:
            with open("Mstsc_data.json","w") as f:
                f.write(json.dumps(Data))
        TC.Destroy()

        

    # 导航栏图片
    def NavigationFormat(self):
        Start = wx.Image(os.getcwd() + "\\icon_\\" + "Start02.png", wx.BITMAP_TYPE_PNG)
        # NStart = Start.Rescale()
        NStart = Start.Rescale(width=18,height=18)
        self.Start = wx.BitmapButton(self.m_toolBar, 1000, NStart.ConvertToBitmap(), wx.DefaultPosition, wx.Size( 25,25 ), wx.BORDER_NONE|wx.TRANSPARENT_WINDOW) # size=(30,30)
        self.m_toolBar.AddControl( self.Start)

        # 这个分割线
        xj = wx.StaticLine(self.m_toolBar,wx.ID_ANY,wx.DefaultPosition, wx.Size(1,25),wx.LI_VERTICAL)
        xj.SetBackgroundColour(wx.Colour(16777215))
        self.m_toolBar.AddControl(xj)

        Add = wx.Image(os.getcwd() + "\\icon_\\" + "Add3.png", wx.BITMAP_TYPE_PNG)
        NAdd = Add.Rescale(width=18,height=18)
        self.Add = wx.BitmapButton(self.m_toolBar, 1001, NAdd.ConvertToBitmap(), wx.DefaultPosition, wx.Size( 25,25 ), wx.BORDER_NONE|wx.TRANSPARENT_WINDOW) # size=(30,30)
        self.m_toolBar.AddControl(self.Add)

        # 这个分割线
        xj = wx.StaticLine(self.m_toolBar,wx.ID_ANY,wx.DefaultPosition, wx.Size(1,25),wx.LI_VERTICAL)
        xj.SetBackgroundColour(wx.Colour(16777215))
        self.m_toolBar.AddControl(xj)

        Xiugai = wx.Image(os.getcwd() + "\\icon_\\" + "Xiugai3.png", wx.BITMAP_TYPE_PNG)
        NXiugai = Xiugai.Rescale(width=18,height=18)
        self.Xiugai = wx.BitmapButton(self.m_toolBar, 1002, NXiugai.ConvertToBitmap(), wx.DefaultPosition, wx.Size( 25,25 ), wx.BORDER_NONE|wx.TRANSPARENT_WINDOW) # size=(30,30)
        self.m_toolBar.AddControl(self.Xiugai)

        shanchu = wx.Image(os.getcwd() + "\\icon_\\" + "shanchu.png", wx.BITMAP_TYPE_PNG)
        Nshanchu = shanchu.Rescale(width=18,height=18)
        self.shanchu = wx.BitmapButton(self.m_toolBar, 1003, Nshanchu.ConvertToBitmap(), wx.DefaultPosition, wx.Size( 25,25 ), wx.BORDER_NONE|wx.TRANSPARENT_WINDOW) # size=(30,30)
        self.m_toolBar.AddControl(self.shanchu)

        chazhao = wx.Image(os.getcwd() + "\\icon_\\" + "chazhao.png", wx.BITMAP_TYPE_PNG)
        Nchazhao = chazhao.Rescale(width=18,height=18)
        self.chazhao = wx.BitmapButton(self.m_toolBar, 1004, Nchazhao.ConvertToBitmap(), wx.DefaultPosition, wx.Size( 25,25 ), wx.BORDER_NONE|wx.TRANSPARENT_WINDOW) # size=(30,30)
        self.m_toolBar.AddControl(self.chazhao)
        
        # 这个分割线
        xj = wx.StaticLine(self.m_toolBar,wx.ID_ANY,wx.DefaultPosition, wx.Size(1,25),wx.LI_VERTICAL)
        xj.SetBackgroundColour(wx.Colour(16777215))
        self.m_toolBar.AddControl(xj)

        biaoji = wx.Image(os.getcwd() + "\\icon_\\" + "biaoji.png", wx.BITMAP_TYPE_PNG)
        Nbiaoji = biaoji.Rescale(width=18,height=18)
        self.biaoji = wx.BitmapButton(self.m_toolBar, 1005, Nbiaoji.ConvertToBitmap(), wx.DefaultPosition, wx.Size( 25,25 ), wx.BORDER_NONE|wx.TRANSPARENT_WINDOW) # size=(30,30)
        self.m_toolBar.AddControl(self.biaoji)

        shangyi = wx.Image(os.getcwd() + "\\icon_\\" + "shangyi02.png", wx.BITMAP_TYPE_PNG)
        Nshangyi = shangyi.Rescale(width=18,height=18)
        self.shangyi = wx.BitmapButton(self.m_toolBar, 1006, Nshangyi.ConvertToBitmap(), wx.DefaultPosition, wx.Size( 25,25 ), wx.BORDER_NONE|wx.TRANSPARENT_WINDOW) # size=(30,30)
        self.m_toolBar.AddControl(self.shangyi)

        xiayi = wx.Image(os.getcwd() + "\\icon_\\" + "xiayi02.png", wx.BITMAP_TYPE_PNG)
        Nxiayi = xiayi.Rescale(width=18,height=18)
        self.xiayi = wx.BitmapButton(self.m_toolBar, 1007, Nxiayi.ConvertToBitmap(), wx.DefaultPosition, wx.Size( 25,25 ), wx.BORDER_NONE|wx.TRANSPARENT_WINDOW) # size=(30,30)
        self.m_toolBar.AddControl(self.xiayi)

        # 这个分割线
        xj = wx.StaticLine(self.m_toolBar,wx.ID_ANY,wx.DefaultPosition, wx.Size(1,25),wx.LI_VERTICAL)
        xj.SetBackgroundColour(wx.Colour(16777215))
        self.m_toolBar.AddControl(xj)

        shezhi = wx.Image(os.getcwd() + "\\icon_\\" + "shezhi.png", wx.BITMAP_TYPE_PNG)
        Nshezhi = shezhi.Rescale(width=18,height=18)
        self.shezhi = wx.BitmapButton(self.m_toolBar, 1008, Nshezhi.ConvertToBitmap(), wx.DefaultPosition, wx.Size( 25,25 ), wx.BORDER_NONE|wx.TRANSPARENT_WINDOW) # size=(30,30)
        self.m_toolBar.AddControl(self.shezhi)

        # 这个分割线
        xj = wx.StaticLine(self.m_toolBar,wx.ID_ANY,wx.DefaultPosition, wx.Size(1,25),wx.LI_VERTICAL)
        xj.SetBackgroundColour(wx.Colour(16777215))
        self.m_toolBar.AddControl(xj)

        self.Write = wx.Button(self.m_toolBar,-1,"UpdateJson",wx.DefaultPosition, wx.Size(75,-1), 0)
        self.m_toolBar.AddControl(self.Write)

        # 写入json
        self.Write.Bind(wx.EVT_LEFT_DOWN, self.OnWriteJson)
    
        self.DHJD()

# wx.grid.EVT_GRID_CELL_CHANGING  单元格内数据发生变化中
# wx.grid.EVT_GRID_CELL_CHANGED   单元格内数据发生变化后
# wx.grid.EVT_GRID_CELL_LEFT_CLICK    左键单击单元格
# wx.grid.EVT_GRID_CELL_LEFT_DCLICK   左键双击单元格
# wx.grid.EVT_GRID_CELL_RIGHT_CLICK   右键单击单元格
# wx.grid.EVT_GRID_CELL_RIGHT_DCLICK  右键双击单元格
# wx.grid.EVT_GRID_SELECT_CELL    选中单元格事件

# wx.grid.GRIDTABLE_NOTIFY_ROWS_DELETED 删除行的消息
# wx.grid.GRIDTABLE_REQUEST_VIEW_GET_VALUES cell 值如果有更改的消息
# wx.grid.GRIDTABLE_NOTIFY_ROWS_INSERTED 行插入的消息
# wx.grid.GRIDTABLE_NOTIFY_ROWS_APPENDED 附近新行的消息

#自定以一个应用程序类
class App(wx.App):
    def OnInit(self):
        #创建窗口对象
        frame=TestFrame()
        # frame.Show()
        return True
        
    def OnExit(self):
        print("应用程序退出")
        return 0

if __name__ == '__main__':
    app=App()#创建自定以对象App
    app.MainLoop()#进入事件主循环
